import axios from 'axios'
import qs from 'qs'
// import router from '@/router'
import conf from '@/conf'
import {Loading, Message} from 'element-ui'
import {popup, popupWhiteOrder} from '../conf/popup'
import store from '@/store'

let loading = ''

export function showFullScreenLoading () {
  if (store.state.needLoadingRequestCount === 0) {
    loading = Loading.service({
      lock: true,
      text: '加载中',
      spinner: 'el-icon-loading',
      background: 'rgba(0, 0, 0, 0.15)'
    })
  }
  store.commit('upadeteLoadingRequestCount', 1)
}

export function tryHideFullScreenLoading () {
  if (store.state.needLoadingRequestCount <= 0) return
  store.commit('upadeteLoadingRequestCount', -1)
  if (store.state.needLoadingRequestCount === 0) {
    setTimeout(() => { loading.close() }, 300)
  }
}

// 请求拦截
axios.interceptors.request.use(config => {
  // 在发送请求之前做些什么
  showFullScreenLoading()

  // config.data = tokenProcess(window.__global_yoonasy_xxxz, config.data)
  return config
}, error => {
  // 对请求错误做些什么
  loading.close()
  return Promise.reject(error)
})

// 响应拦截
axios.interceptors.response.use(response => {
  // 对响应数据做些事
  tryHideFullScreenLoading()
  if (response.data && !response.data.success) {
    // 接口GetEmployeeInfoLogin作为同步服务器登录状态和权限控制的路由守卫的request，其登录超时拦截和逻辑单独处理，以免不必要的message提示
    // if (response.data.Code === -100 && response.config.url !== conf.baseURL + '/DPM/Employee/GetEmployeeInfoLogin') {
    //   Message({
    //     showClose: true,
    //     message: '您的登录已失效，请重新登录',
    //     type: 'error'
    //   })
    //   router.push({path: '/login'})
    // }
    // 错误状态码弹窗
    let flag = true
    popupWhiteOrder.forEach(e => { if (conf.baseURL + e === response.config.url) flag = false })
    flag && (response.config.popup !== false) && popup(response.data)
  }
  return response
}, error => {
  loading.close()
  // 对响应错误做点什么
  Message({
    showClose: true,
    message: '服务器繁忙，请稍后再试',
    type: 'error'
  })
  return Promise.resolve(error)
})

export default {
  post (url, data, options) {
    return new Promise((resolve, reject) => {
      let axiosConfig = {
        method: 'post',
        baseURL: conf.baseURL,
        url,
        data: qs.stringify(data),
        timeout: conf.timeout,
        headers: {
          'X-Requested-With': 'XMLHttpRequest',
          'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
        }
      }
      // 合并传递进来的自定义选项
      axiosConfig = Object.assign(axiosConfig, options)
      axios(axiosConfig).then(response => {
        resolve(response.data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  get (url, params, options) {
    return new Promise((resolve, reject) => {
      let axiosConfig = {
        method: 'get',
        baseURL: conf.baseURL,
        url,
        params,
        timeout: conf.timeout,
        headers: {
          'X-Requested-With': 'XMLHttpRequest',
          'Content-Type': 'application/json; charset=utf-8'
        }
      }
      // 合并传递进来的自定义选项
      axiosConfig = Object.assign(axiosConfig, options)
      axios(axiosConfig).then(response => {
        resolve(response.data)
      }).catch(error => {
        reject(error)
      })
    })
  }
}
